﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class FrmThanhvien : PRE.Catalog.FrmBase
    {
        ServiceReference_Thanhvien.WS_ThanhvienSoapClient sr_tv = new ServiceReference_Thanhvien.WS_ThanhvienSoapClient();
        ServiceReference_Vaitro.WS_VaitroSoapClient sr_vt = new ServiceReference_Vaitro.WS_VaitroSoapClient();
        IList<ServiceReference_Thanhvien.Thanhvien> list;

        WS.BLL.EntitiesBLL.ThanhvienBLL _bll = new WS.BLL.EntitiesBLL.ThanhvienBLL();

        public FrmThanhvien()
        {
            InitializeComponent();
        }

        private void FrmThanhvien_Load(object sender, EventArgs e)
        {
            PerformRefresh();
            
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);
            ResetText();
            lbMsg.Visible = true;

            txtTaikhoan.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id = Convert.ToInt32(grvMain.GetFocusedRowCellValue("id"));

            if (MessageBox.Show("Bạn có muốn xóa không ?", "Thông Báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                var ok = sr_tv.DeleteThanhvien(id);
                if (ok)
                    PerformRefresh();
                else
                    MessageBox.Show("Lỗi! Vui lòng kiểm tra lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);

            txtMatkhau.Focus();
            txtTaikhoan.Properties.ReadOnly = true;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();

            if (list != null)
            {
                // Binding data
                ClearDataBindings();
                if (list.Count > 0) DataBindingControl();
            }

            lbMsg.Visible = false;

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (IsAdd)
            {
                if (txtTaikhoan.Text == "" || txtMatkhau.Text == "" || txtHoten.Text == "")
                {
                    MessageBox.Show("Bạn phải điền đầy đủ thông tin cho thành viên !!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    PerformAdd();
                }
                else
                {
                    ServiceReference_Thanhvien.Thanhvien tv = new ServiceReference_Thanhvien.Thanhvien();

                    tv.taikhoan = txtTaikhoan.Text;
                    tv.matkhau = _bll.EncodeMD5(txtMatkhau.Text);
                    tv.hoten = txtHoten.Text;
                    tv.vaitro_id = Convert.ToInt32(cboVaitro.EditValue);


                    var ok = sr_tv.InsertThanhvien(tv);
                    if (ok)
                    {
                        PerformRefresh();
                        PerformAdd();
                        lbMsg.Text = " Thành viên " + tv.hoten + " đã được thêm thành công";
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Vui lòng kiểm tra lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtTaikhoan.Focus();
                    }
                }
            }

            if (IsEdit)
            {
                var _id = Convert.ToInt32(grvMain.GetFocusedRowCellValue("id"));

                ServiceReference_Thanhvien.Thanhvien tv = new ServiceReference_Thanhvien.Thanhvien();

                tv.id = _id;
                tv.matkhau = _bll.EncodeMD5(txtMatkhau.Text);
                tv.hoten = txtHoten.Text;
                tv.vaitro_id = Convert.ToInt32(cboVaitro.EditValue);

                var ok = sr_tv.UpdateThanhvien(tv);
                if (ok)
                {
                    PerformRefresh();
                    ReadOnlyControl(); ChangeStatus();
                    lbMsg.Text = " Thành viên " + tv.hoten + " đã được sửa thông tin thành công";
                }
                else
                {
                    MessageBox.Show("Lỗi! Vui lòng kiểm tra lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtMatkhau.Focus();
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtHoten.Text = null;
            txtMatkhau.Text = null;
            cboVaitro.ItemIndex = -1;
            txtTaikhoan.Text = null;

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtTaikhoan.DataBindings.Clear();
            txtHoten.DataBindings.Clear();
            txtMatkhau.DataBindings.Clear();
            cboVaitro.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtTaikhoan.DataBindings.Add("EditValue", list, ".taikhoan");
            txtHoten.DataBindings.Add("EditValue", list, ".hoten");
            txtMatkhau.DataBindings.Add("EditValue", list, ".matkhau");
            cboVaitro.DataBindings.Add("EditValue", list, ".vaitro_id");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtTaikhoan.Properties.ReadOnly = isReadOnly;
            txtHoten.Properties.ReadOnly = isReadOnly;
            txtMatkhau.Properties.ReadOnly = isReadOnly;
            cboVaitro.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            
            list = (IList<ServiceReference_Thanhvien.Thanhvien>)sr_tv.ReadList();

            IList<ServiceReference_Vaitro.Vaitro> list_vt;
            list_vt = (IList<ServiceReference_Vaitro.Vaitro>)sr_vt.ReadList();
            cboVaitro.Properties.DataSource = list_vt;

            if (list != null)
                grcMain.DataSource = list;

            base.LoadData();
        }
        #endregion

        private void txtTaikhoan_TextChanged(object sender, EventArgs e)
        {
            ServiceReference_Thanhvien.Thanhvien tv = new ServiceReference_Thanhvien.Thanhvien();
            tv.taikhoan = txtTaikhoan.Text;
            IList<ServiceReference_Thanhvien.Thanhvien> tb2 = sr_tv.CheckThanhvien(tv);
                
            if (tb2.Count > 0)
            {
                lbMsg.Text = "Tài khoản đã tồn tại trong hệ thống, vui lòng nhập lại";
                txtTaikhoan.Focus();
            }
            else
                lbMsg.Text = "";
         
        }





    }
}
